/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.form;

import domen.Fakultet;
import domen.Kontakt;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import model.Storage;
import model.StorageBroker;
import ui.components.modeltabele.KontaktModelTabele;

/**
 *
 * @author lukab_000
 */
public class JPanelFakultet extends javax.swing.JPanel {

    JPanelFakultetiPrikaz prikazFakulteta;
    StorageBroker sb;
    Fakultet odabranifakultet;
    String imeFakulteta;
    String adresaFakulteta;

    KontaktModelTabele kmt;
//    String brojTelefonaFakulteta;
//    String emailFakulteta;
//    String opis;
//    int brojFakulteta;

    /**
     * Creates new form JPanelFakultet
     */
    public JPanelFakultet() {
        initComponents();
        srediFormu();
        jbtnIzmeni.setVisible(false);
        jbtnObrisi.setVisible(false);
        jpanelKontakti.setVisible(false);
    }

    public JPanelFakultet(Fakultet f) {
        initComponents();
        srediFormu();
        jbtnSacuvaj.setVisible(false);
        sb = new StorageBroker(new Fakultet());

        odabranifakultet = f;
//        prikazFakulteta = panel;
        if (odabranifakultet == null) {
            System.out.println("odabrani fax je null");
        }
        jPImeFakulteta.vratiKomponentuTexta().setText(odabranifakultet.getNaziv());
        jPAdresaFakulteta.vratiKomponentuTexta().setText(odabranifakultet.getAdresa());

        kmt = new KontaktModelTabele(f);
        jTblKontakti.setModel(kmt);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPAtributi = new javax.swing.JPanel();
        jPImeFakulteta = new ui.components.JPanelTextField();
        jPAdresaFakulteta = new ui.components.JPanelTextField();
        jPanel1 = new javax.swing.JPanel();
        jbtnSacuvaj = new javax.swing.JButton();
        jbtnIzmeni = new javax.swing.JButton();
        jbtnObrisi = new javax.swing.JButton();
        jpanelKontakti = new javax.swing.JPanel();
        txtTelefon = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTblKontakti = new javax.swing.JTable();
        txtSluzba = new javax.swing.JTextField();
        btnDodajKontakt = new javax.swing.JButton();

        jbtnSacuvaj.setText("Sacuvaj");
        jbtnSacuvaj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSacuvajActionPerformed(evt);
            }
        });

        jbtnIzmeni.setText("Izmeni");
        jbtnIzmeni.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnIzmeniActionPerformed(evt);
            }
        });

        jbtnObrisi.setText("Obrisi");
        jbtnObrisi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnObrisiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jbtnIzmeni, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtnSacuvaj, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtnObrisi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbtnSacuvaj)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnIzmeni)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnObrisi))
        );

        javax.swing.GroupLayout jPAtributiLayout = new javax.swing.GroupLayout(jPAtributi);
        jPAtributi.setLayout(jPAtributiLayout);
        jPAtributiLayout.setHorizontalGroup(
            jPAtributiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPAtributiLayout.createSequentialGroup()
                .addGroup(jPAtributiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPImeFakulteta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPAdresaFakulteta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17))
        );
        jPAtributiLayout.setVerticalGroup(
            jPAtributiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPAtributiLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPAtributiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPAtributiLayout.createSequentialGroup()
                        .addComponent(jPImeFakulteta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPAdresaFakulteta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jpanelKontakti.setBorder(javax.swing.BorderFactory.createTitledBorder("Kontakti"));

        jLabel2.setText("Sluzba / lice :");

        jLabel1.setText("Telefon:");

        jTblKontakti.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTblKontakti);

        btnDodajKontakt.setText("Dodaj kontakt");
        btnDodajKontakt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDodajKontaktActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpanelKontaktiLayout = new javax.swing.GroupLayout(jpanelKontakti);
        jpanelKontakti.setLayout(jpanelKontaktiLayout);
        jpanelKontaktiLayout.setHorizontalGroup(
            jpanelKontaktiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanelKontaktiLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpanelKontaktiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 324, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jpanelKontaktiLayout.createSequentialGroup()
                        .addGroup(jpanelKontaktiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jpanelKontaktiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtSluzba, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                            .addComponent(txtTelefon))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDodajKontakt)))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        jpanelKontaktiLayout.setVerticalGroup(
            jpanelKontaktiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanelKontaktiLayout.createSequentialGroup()
                .addGroup(jpanelKontaktiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTelefon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(btnDodajKontakt))
                .addGap(7, 7, 7)
                .addGroup(jpanelKontaktiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtSluzba, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPAtributi, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jpanelKontakti, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPAtributi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpanelKontakti, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
/**
     * cuva objekat fakultet u hashmapu
     *
     * @param evt
     */
    private void jbtnSacuvajActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSacuvajActionPerformed
        uzmiPodatkeSaForme();

        if (validacijaFakulteta()) {
            Fakultet fakultet = new Fakultet();
            fakultet.setNaziv(imeFakulteta);
            fakultet.setAdresa(adresaFakulteta);

//            fakultet.setOpisFakulteta(opis);
//            fakultet.setBrojTelefona(brojFakulteta);
//            fakultet.setEmail(emailFakulteta);
            StorageBroker sb1 = new StorageBroker(fakultet);
            sb1.ubaciEntitetUMapu();
            sb1.ispisiMapu();
            JOptionPane.showMessageDialog(this, "Fakultet je uspesno sacuvan");
            
            postaviPocetneVrednosti();
        }
    }//GEN-LAST:event_jbtnSacuvajActionPerformed
    /**
     * menja objekat iz hashmape
     *
     * @param evt
     */
    private void jbtnIzmeniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnIzmeniActionPerformed
        uzmiPodatkeSaForme();

        if (validacijaFakulteta()) {
//      iymenio sam ovo, ybog ovoga je pravilu duple instance fakulteta
//            Fakultet fakultet = new Fakultet();
//            fakultet.setNaziv(imeFakulteta);
//            fakultet.setAdresa(adresaFakulteta);
//            sb.izbrisiEntitet(odabranifakultet.getNaziv());
//            sb.izmeniEntitet(fakultet.getNaziv(), fakultet);
           
            odabranifakultet.setNaziv(imeFakulteta);
            odabranifakultet.setAdresa(adresaFakulteta);
//            sb.izmeniEntitet(odabranifakultet.getNaziv(), odabranifakultet);

//            prikazFakulteta.osveziTabelu();
            JOptionPane.showMessageDialog(this, "Podaci o fakultetu su uspesno izmenjeni.");
            sb.ispisiMapu();
            // zasto ovo ??
            Storage.getInstance().getGlavnaForma().ucitajPanel(new JPanelFakultetiPrikaz());

        }
    }//GEN-LAST:event_jbtnIzmeniActionPerformed
    /**
     * brise objekat iz hashmape
     *
     * @param evt
     */
    private void jbtnObrisiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnObrisiActionPerformed
        sb.izbrisiEntitet(odabranifakultet.getNaziv());
        JOptionPane.showMessageDialog(this, "Fakultet je uspesno izbrisan.");

        Storage.getInstance().getGlavnaForma().ucitajPanel(new JPanelFakultetiPrikaz());
    }//GEN-LAST:event_jbtnObrisiActionPerformed

    private void btnDodajKontaktActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDodajKontaktActionPerformed

        Kontakt k = new Kontakt();
        k.setTelefon(Long.parseLong(txtTelefon.getText()));
        k.setOpis(txtSluzba.getText());

        odabranifakultet.getKontakti().add(k);
        kmt.fireTableDataChanged();

    }//GEN-LAST:event_btnDodajKontaktActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDodajKontakt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private ui.components.JPanelTextField jPAdresaFakulteta;
    private javax.swing.JPanel jPAtributi;
    private ui.components.JPanelTextField jPImeFakulteta;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTblKontakti;
    private javax.swing.JButton jbtnIzmeni;
    private javax.swing.JButton jbtnObrisi;
    private javax.swing.JButton jbtnSacuvaj;
    private javax.swing.JPanel jpanelKontakti;
    private javax.swing.JTextField txtSluzba;
    private javax.swing.JTextField txtTelefon;
    // End of variables declaration//GEN-END:variables

    /**
     * proverava da li su sva polja uneta i ako nisu aktivira labele koje
     * crvenim slovima daju opis problema
     */
    private boolean validacijaFakulteta() {

        boolean validacija = true;

        try {
            if (imeFakulteta.isEmpty()) {
                jPImeFakulteta.vratiKomponentuZaPrikazGreske().setText("Polje naziv mora biti uneto!");
                //throw new Exception("Validacija nije uspela");
                validacija = false;
            } else {
                jPImeFakulteta.vratiKomponentuZaPrikazGreske().setText(" ");
            }

            if (adresaFakulteta.isEmpty()) {
                jPAdresaFakulteta.vratiKomponentuZaPrikazGreske().setText("Polje adresa  mora biti "
                        + "uneto!");
                validacija = false;
            } else {
                jPAdresaFakulteta.vratiKomponentuZaPrikazGreske().setText(" ");
            }

//            if (brojTelefonaFakulteta.isEmpty()) {
//                jPBrojTelefonaFakulteta.vratiKomponentuZaPrikazGreske().setText("Polje broj telefona  "
//                        + "mora biti uneto!");
//                validacija = false;
//            } else {
//                
//                jPBrojTelefonaFakulteta.vratiKomponentuZaPrikazGreske().setText(" ");
//            }
//           
//
//            if (emailFakulteta.isEmpty()) {
//                jPMailFakulteta.vratiKomponentuZaPrikazGreske().setText("Polje email mora biti uneto!");
//                validacija = false;
//            } else {
//                jPMailFakulteta.vratiKomponentuZaPrikazGreske().setText(" ");
//            }
//            
//            if (opis.isEmpty()) {
//                jPanelOpisFakul1.vratiGresku().setText("Polje opis mora biti popunjeno");
//                validacija = false;
//            } else {
//                jPanelOpisFakul1.vratiGresku().setText(" ");
//            }
//            
//            try {
//                brojFakulteta = Integer.parseInt(brojTelefonaFakulteta);
//            } catch (Exception e) {
//                 jPBrojTelefonaFakulteta.vratiKomponentuZaPrikazGreske().setText("U Polje broj telefona  "
//                        + "mora biti unet broj");
//                validacija = false;
//            }
//
//           
            return validacija;
        } catch (Exception e) {
            System.out.println("Validacija nije uspela!");
            JOptionPane.showMessageDialog(this, e.getMessage());
        }
        return false;
    }

    /**
     * funkcija sredjuje polja na formi tako sto labelama i tekstualnim poljima
     * daje vrednosti koje zelimo
     */
    private void srediFormu() {
        jPImeFakulteta.vratiKomponentuZaPrikazTeksta().setText("Naziv : ");
        jPImeFakulteta.vratiKomponentuZaPrikazGreske().setText("  ");

        jPAdresaFakulteta.vratiKomponentuZaPrikazTeksta().setText("Adresa : ");
        jPAdresaFakulteta.vratiKomponentuZaPrikazGreske().setText(" ");

//          jPBrojTelefonaFakulteta.vratiKomponentuZaPrikazTeksta().setText("Broj telefona: ");
//          jPBrojTelefonaFakulteta.vratiKomponentuZaPrikazGreske().setText(" ");
//          
//          jPMailFakulteta.vratiKomponentuZaPrikazTeksta().setText("Email :");
//          jPMailFakulteta.vratiKomponentuZaPrikazGreske().setText(" ");
//          
//          jPanelOpisFakul1.vratiGresku().setText(" ");
        postaviPocetneVrednosti();
    }

    /**
     * postavlja pocetne vrednosti na text filed-ove za atribute fakulteta
     */
    private void postaviPocetneVrednosti() {
        jPImeFakulteta.vratiKomponentuTexta().setText(" ");
        jPAdresaFakulteta.vratiKomponentuTexta().setText(" ");
//       jPBrojTelefonaFakulteta.vratiKomponentuTexta().setText(" ");
//       jPMailFakulteta.vratiKomponentuTexta().setText(" ");
//       jPanelOpisFakul1.vratiOpis().setText("");
    }

    /**
     * uzima podatke sa forme i postavlja ih kao atribute fakulteta
     */
    private void uzmiPodatkeSaForme() {

        imeFakulteta = jPImeFakulteta.vratiKomponentuTexta().getText().trim();
        adresaFakulteta = jPAdresaFakulteta.vratiKomponentuTexta().getText().trim();
//        brojTelefonaFakulteta = jPBrojTelefonaFakulteta.vratiKomponentuTexta().getText().trim();
//        opis = jPanelOpisFakul1.vratiOpis().getText().trim();
//                
//        emailFakulteta = jPMailFakulteta.vratiKomponentuTexta().getText().trim();
    }

}
